package com.lap.auth.adapter.driven.web.user;

import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lap.auth.application.user.UserRoleAppService;
import com.lap.auth.application.user.command.AssignRoleCmd;
import com.lap.watchdog.starter.annotation.RepeatSubmit;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

/**
 * 角色授权
 *
 * @author Shuisheng Lao(劳水生)
 * @version 0.0.1
 */
@Tag(name = "用户授权角色管理", description = "提供用户绑定角色，已绑定角色查询")
@RequiredArgsConstructor
@RestController
@RequestMapping("api/v1/users")
public class UserRoleController {

  private final UserRoleAppService userRoleAppService;

  @Operation(summary = "用户授权角色(一级权限)")
  @RepeatSubmit
  @PutMapping("{userId}/roles")
  public void assignRoles(@PathVariable Integer userId, @RequestBody AssignRoleCmd cmd) {
    userRoleAppService.assignRoles(userId, cmd.getRoleIdSet());
  }

  @Operation(summary = "获取已授权角色")
  @GetMapping("{userId}/roles")
  public List<Integer> getRolesByUserId(@PathVariable Integer userId) {
    return userRoleAppService.queryRolesByUserId(userId);
  }
}
